package com.keyancompany.zhongbaopingtai.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.activity.DocumentShareActivity;
import com.keyancompany.zhongbaopingtai.adapter.DocumentsAdapter;
import com.keyancompany.zhongbaopingtai.model.SharePager;
import com.keyancompany.zhongbaopingtai.utils.DebugLog;
import com.keyancompany.zhongbaopingtai.weight.EmptyView;

/**
 * Created by Taylor on 11/8/16.
 */

public class DocsFragment extends Fragment {

    private static final String ARG_COLUMN_COUNT = "column-count";
    private static final String TYPE = "type";
    private OnListFragmentInteractionListener mListener;

    private int mColumnCount = 1;
    private int type = 1;//个人 1   团队 2

    public DocsFragment() {
    }

    public static DocsFragment newInstance(int columnCount, int type) {
        DocsFragment fragment = new DocsFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_COLUMN_COUNT, columnCount);
        args.putInt(TYPE, type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
            type = getArguments().getInt(TYPE);
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_documents, container, false);
        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.list);
        EmptyView emptyView = (EmptyView) view.findViewById(R.id.empty_view);

        if (mColumnCount <= 1) {
            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        } else {
            recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), mColumnCount));
        }

        if (((DocumentShareActivity) getActivity()).getList() != null) {
            recyclerView.setAdapter(new DocumentsAdapter(((DocumentShareActivity) getActivity()).getList(),
                    mListener));
        } else {
        }

        return view;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof OnListFragmentInteractionListener) {
            mListener = (OnListFragmentInteractionListener) activity;
        } else {
            throw new RuntimeException(activity.toString()
                    + " must implement OnListFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface OnListFragmentInteractionListener {
        // TODO: Update argument type and name
        void onListFragmentInteraction(SharePager pager);
    }
}
